﻿using CuaHangLego.Bussiness;
using CuaHangLego.DataAccess;
using CuaHangLego.Model;
using CuaHangLego.Webform.App_Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CuaHangLego.Webform
{
    public partial class AddToCart : BasePage
    {
        public IList<int> lint = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        ShoppingCartBSS bss = new ShoppingCartBSS();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IList<int> l = Session["cart"] as IList<int>;
                if (l != null)
                {
                    IList<CTHD> cthds=new List<CTHD>();
                    l = l.Distinct().ToList();
                    IList<SanPham> lsp = new List<SanPham>();
                    foreach (int i in l)
                    {
                        SanPhamKho sp = SanPhamKhoDA.Instance.GetByID(i);
                        lsp.Add(sp);
                        CTHD ct = new CTHD();
                        ct.SanPham = sp;
                        ct.SoLuong = 0;
                        cthds.Add(ct);
                    }
                    Session["shopping"] = cthds;
                    CartProducts.DataSource = lsp;
                    CartProducts.DataBind();
                }

            }
        }

        protected void CartProducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DropDownList l = ((DropDownList)e.Item.FindControl("SoLuongCbx"));
            }
        }

        protected void SoLuongCbx_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = sender as DropDownList;
            if (ddl != null)
            {
                RepeaterItem repeaterRow = (RepeaterItem)ddl.Parent;
                Label label = (Label)repeaterRow.FindControl("ThanhTien");
                HiddenField hf = (HiddenField)repeaterRow.FindControl("ProductID");
                SanPham sp = SanPhamDA.Instance.GetByID(int.Parse(hf.Value));
                int SoLuong = int.Parse(ddl.SelectedValue);
                label.Text = String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:n0}.000", sp.GiaBan * SoLuong);
                IList<CTHD> cthds = Session["shopping"] as IList<CTHD>;
                foreach (CTHD ct in cthds)
                {
                    if (ct.SanPham.MaSo == sp.MaSo)
                    {
                        ct.SoLuong = SoLuong;
                        break;
                    }
                }
                int tong = bss.CalculateTotal(cthds);
                TongGia.Text = String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:n0}.000", tong);
                TongHoaDon.Text = String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:n0}.000", tong);
            }
        }

        protected void RemoveCart_Command(object sender, CommandEventArgs e)
        {
            Session["cart"] = null;
            Response.Redirect("~");
        }

        protected void SubmitBuy_Command(object sender, CommandEventArgs e)
        {
            if (UserAccess.IsLogin())
            {
                IList<CTHD> cthds = Session["shopping"] as IList<CTHD>;
                bool ok = true;
                foreach (CTHD ct in cthds)
                {
                    if (!bss.CheckQuantity(ct))
                    {
                        string a = string.Format("alert('Số lượng sản phẩm {0} không đủ!')", ct.SanPham.Ten.Replace("\'", "\\'"));
                        ok = false;
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", a, true);
                        break;
                    }
                }
                if (ok)
                    if (bss.CheckOut(UserAccess.currentUser, cthds))
                    {
                        Session["cart"] = null;
                        Response.Redirect("Notice.aspx?type=CheckoutSuccess");
                    }
                    else
                    {
                        string a = string.Format("alert('Thanh toán không thành công! Vui lòng kiểm tra lại thông tin!')");
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", a, true);
                    }
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Vui lòng đăng nhập để thực hiện thanh toán')", true);
            }
        }
    }
}